﻿#include"LTC2943.h"

#define R_SENSE 50.0//检测电阻50毫欧

#define Prescaler_M 4096//分频器值

//电池容量
#define battery_capacity 3000//单位：毫安时 mAh



float LTC2943_Accumulated_Charge_calculation(uint8_t* Accumulated_Charge_Register)
{
    float q_LSB = 0.34 * 50.0 / R_SENSE * Prescaler_M / 4096;//一量程代表的电量 单位：毫安时 mAh
    printf("q_LSB = %f\n", q_LSB);
    uint16_t RESULT = (Accumulated_Charge_Register[0] << 8) | Accumulated_Charge_Register[1];
    float Q_BAT = RESULT * q_LSB;
    return Q_BAT;
}

float LTC2943_Voltage_calculation(uint8_t* Voltage_Registers)
{
    uint16_t RESULT = (Voltage_Registers[0] << 8) | Voltage_Registers[1];
    float V_SENSE = RESULT * (23.6 / 65535);//单位：伏特 V
    return V_SENSE;
}

float LTC2943_Current_calculation(uint8_t* Current_Registers)
{
    uint16_t RESULT = (Current_Registers[0] << 8) | Current_Registers[1];
    float I_BAT = (RESULT - 32767) * (60.0 / R_SENSE / 32767);//单位：安培 A
    return I_BAT;
}

float LTC2943_Temperature_calculation(uint8_t* Temperature_Registers)
{
    uint16_t RESULT = (Temperature_Registers[0] << 8) | Temperature_Registers[1];
    float Temperature = RESULT * (510.0 / 65535) - 273.15;//得到摄氏温度℃
    return Temperature;
}


